package com.leyou.trade.web;

import com.leyou.trade.dto.OrderDTO;
import com.leyou.trade.dto.OrderFormDTO;
import com.leyou.trade.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * (Order)表控制层
 *
 * @author makejava
 * @since 2020-08-17 14:53:56
 */
@RestController
@RequestMapping("order")
public class OrderController {
    /**
     * 服务对象
     */
    @Autowired
    private OrderService orderService;

    /**
     * 提交订单
     * @param orderFormDTO 订单数据
     * @return 订单id
     */
    @PostMapping
    public ResponseEntity<Long> createOrder(@RequestBody OrderFormDTO orderFormDTO){
        Long orderId = orderService.createOrder(orderFormDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(orderId);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<OrderDTO> selectOne(@PathVariable("id") Long id) {
        return ResponseEntity.ok(new OrderDTO(orderService.getById(id)));
    }

    /**
     * 根据id查询订单状态
     * @param id 订单id
     * @return 状态
     */
    @GetMapping("/status/{id}")
    public ResponseEntity<Integer> queryOrderStatus(@PathVariable("id") Long id) {
        return ResponseEntity.ok(orderService.queryOrderStatus(id));
    }
}